/**
 * 
 */
package org.lookouster.mopg.bo;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
import java.util.HashMap;

import org.lookouster.mopg.connector.Database;

/**
 * @author lkster
 *
 */
public class Message {
	
	Bill relatedBill;
	/**type
	 * 1 if first message containing bill_no 
	 * 2 if bill payment confirmation 
	 * 0 for non qualified message
	 */
	String type;
	String customerNo;
	String msgDate;
	String text;
	ServiceProvider sp;
	
	/**
	 * 
	 * @param t: Type
	 * @param b: Related bill
	 * @param num: GSM number
	 * @param d: Message date
	 * @param content: SMS Text
	 * @param s: Service provider
	 */
	public Message(String t, Bill b, String num, String d, String content, ServiceProvider s)
	{
		type = t;
		relatedBill = b;
		customerNo = num;
		msgDate = d;
		text = content;
		sp = s;
		
	}
	
	/**
	 * 
	 * @param message
	 * @param num : The GSM number sending or receiving
	 * @param d: Message date
	 */
	public Message(String message, String num, String d)
	{
		text = message;
		sp = new ServiceProvider(message.split(" ")[0]);
		relatedBill = Bill.getBillByNo(message.split(" ")[1], sp.providerId);
		if(message == "1")
			type = "IN_2";
		else
		{
			if(sp.providerId == 0)
				type = "IN_0";//Provider not understood
			if(relatedBill == null)
				type = "IN_0";//Non existing bill number
		}
		if(sp.providerId != 0 & relatedBill != null)
			type = "IN_1";
		customerNo = num;
		msgDate = d;
		
	}
	
	public static HashMap<Integer,Message> retrieveWaitingList() throws SQLException
	{
		HashMap<Integer, Message> waitingList = new HashMap<Integer,Message>();
		ResultSet rs = Database.retrieveData("Select * from smsserver_in");
		while(rs.next())
			waitingList.put(rs.getInt("id"),new Message(rs.getString("text"),rs.getString("originator"), rs.getString("receive_date")));
		return waitingList;
	}
	
	public void replyTo()
	{
		
	}

}
